agenda.admin = new liwe.module ( "agenda.admin", null, "Gestione agenda" );

agenda.admin._dest_div = 'system-content';
agenda.admin._currents_cals = {};
agenda.admin._selected_cal = '';
agenda.admin._users_opts = [];
agenda.admin._groups_opts = [];
agenda.admin._calendars_opts = [];
agenda.admin._fields = {
		'users' : [ 'uid', 'login' ],
		'groups' : [ 'gid', 'descr' ],
		'calendars' : [ 'id_cal', 'cal_name' ]
		};
agenda.admin._tabs_div = 'AG-tabs-cnt';
agenda.admin._add_form_div = 'AG-cal-add';
agenda.admin._filter_form_div = 'AG-cal-filter';
agenda.admin._admin_cals_div = 'AG-cal-am';

agenda.admin._init_options = function ( cback, refresh )
{
	if ( agenda._cache.count () > 0 && agenda._cache.users && ! refresh )
	{
		if ( cback ) cback ();
		return;
	}
	
	agenda._cache = {};
	post_user = { action : 'user.admin.list', uid : '' };
	post_group = { action: 'user.group_admin.list', gid : '', name : '', descr : '' };
	post_cal = { action: 'agenda.ajax.get_calendar', id_cal : '', cal_name : '', uid : '', gid : '' };

	liwe.AJAX.add ( 'agenda_options', liwe.AJAX.url, post_user, function ( data ) { agenda._cache [ 'users' ] = data.get ( 'users' ); } ); 
	liwe.AJAX.add ( 'agenda_options', liwe.AJAX.url, post_group, function ( data ) { agenda._cache [ 'groups' ] = data.get ( 'groups' ); } ); 
	liwe.AJAX.add ( 'agenda_options', liwe.AJAX.url, post_cal, function ( data ) { agenda._cache [ 'calendars' ] = data.get ( 'calendar' ); } ); 
	
	liwe.AJAX.start ( 'agenda_options', function ()
		{
			agenda._cache.iterate ( function ( v, k ) { agenda._create_options ( k ); } );
			if ( cback ) cback ();
		} );
};

agenda.admin._init_tabs = function ( dest_div )
{
        var s = new String.buffer ();
        var res, name = 'agenda';
        var prefix = name + '-tabs';
        if ( ! dest_div ) dest_div = agenda.admin._tabs_div;

        if ( ! agenda.admin.tabs ) agenda.admin.tabs = new OS3Tabs.instance ( prefix );

        var filter_name = agenda.admin._filter_form_div; 
        var add_name = agenda.admin._add_form_div;

        agenda.admin.tabs.clear ();

        agenda.admin.tabs.set_group ( name + '-tabs' );

        $( dest_div, 'Initializing tabs ...' );
        s.add ( String.formatDict ( agenda.templates [ 'tabs_start' ], { 'prefix' : prefix  } ) );

        s.add ( String.formatDict ( agenda.templates [ 'tabs_tab' ], { "tab_name" : filter_name } ) );
        agenda.admin.tabs.add ( 'Filtri', filter_name );

	s.add ( String.formatDict ( agenda.templates [ 'tabs_tab' ], { "tab_name" : add_name } ) );
	agenda.admin.tabs.add ( 'Edit', add_name );

        s.add ( agenda.templates [ 'tabs_end' ] );

        res = s.toString ();

        $( dest_div, res );

        agenda.admin.tabs.render ();

        agenda.admin.tabs.enable_tab ( add_name, 'enabled' );
        agenda.admin.tabs.enable_tab ( filter_name, 'enabled' );
        agenda.admin.tabs.send_event ( filter_name, 'click' );
};

agenda.admin.render = function ()
{
	if ( $( 'AG-cnt' ) ) return;
	var cnt = agenda.templates [ 'cal-cnt' ];
	$( agenda.admin._dest_div , String.formatDict ( agenda.templates [ 'layout' ], { 'cal_cnt' : cnt } ) );	
	agenda.admin._init_options ( function ()
		{
			agenda.admin._init_tabs ();
			agenda.admin._create_add_cal_form ();
			agenda.admin._init_admin_man ( agenda.admin._admin_cals_div );
			agenda.admin.admin_cals.show_list ();
		} );
};

agenda.admin.init = function ()
{
	if ( ! agenda.templates ) { agenda.load_templates (); } 

        var _acts = [
                        { func: function () { agenda.admin.render (); }, img_src: '', label: 'Gestione Calendari', "class" : "" },
			{ func: function () { agenda._exit (); }, label : 'unload' }
                ];

        var _plugin = [];
	/*
                {
                        label:   "Calendario",
                        handler: 
                        context: 
                }
        ];
	*/
        agenda.admin.register ( _acts, _plugin );
	
	console.debug ( "Agenda Admin init" );
};

agenda.admin._init_admin_man = function ( dest_div )
{
	if ( agenda.admin.admin_cals ) return;
	agenda.admin.admin_cals = new AdminManager ( {
			'dest_res' : dest_div,
			'list_action' : 'agenda.ajax.get_calendar',
			'del_action' : 'agenda.admin.del_calendar',
			'mod_menu_actions' : [
					{ func: 'agenda.admin.add_cal', 'class' : 'add', label: 'Nuovo calendario' }
						],
			'filter_form' : agenda.admin._filter_form,
			'filter_form_name' : 'agenda-cals-filter'
			} );

	agenda.admin.admin_cals.set_row_fields (
			[
				[ 'ID', 'id_cal' ],
				[ 'Nome', 'cal_name' ],
				[ 'Utente', '_login' ],
				[ 'Gruppo', '_group_name' ]
			] );

	agenda.admin.admin_cals.cbacks [ 'row_manip' ] = agenda.admin._cals_row_manip;
	agenda.admin.admin_cals.cbacks [ 'edit_item' ] = function ( id ) { agenda.admin.add_cal ( dest_div, id ) };
	agenda.admin.admin_cals.cbacks [ 'del_item' ] = function ( id ) { agenda.admin.del_cal ( dest_div, id ) };

};

agenda.admin._cals_row_manip = function ( ds, row )
{
	row [ '_id' ] = row [ 'id_cal' ];
	row [ '_login' ] = agenda._users.get ( row.uid, '' ); 
	row [ '_group_name' ] = agenda._groups.get ( row.gid, '' ); 
};

agenda.admin._filter_form = function ()
{
	var f = new liwe.form.instance ( 'agenda-cals-filter' );
	f.label ( { value: 'Nome calendario' } );
	f.text ( { name: 'cal_name' } );
	f.label ( { value: 'Assegnato a:' } );
	f.select ( { name: 'uid', options: agenda.admin._users_opts, force_select : true } );
	f.label ( { value: 'Assegnato al gruppo:' } );
	f.select ( { name: 'gid', options: agenda.admin._groups_opts, force_select : true } );
	f.button ( { value: "Applica Filtro", "class": 'green',  onclick: "agenda.admin._filter_apply();", nonl: true } );
	f.button ( { value: 'Reset filtri', 'class' : 'yellow', onclick: function ( f ) { f.clear (); } } );
	f.set ( agenda.admin._filter_form_div );
};

agenda.admin._filter_apply = function ()
{
	agenda.admin.admin_cals.search_start ();
};


agenda.admin._create_add_cal_form = function ( data )
{
	var f = new liwe.form.instance ( 'agenda_add_cal' );
	f.hidden ( 'id_cal', agenda.admin._selected_cal );
	f.hidden ( 'action', 'agenda.admin.add_calendar' );
	f.label ( { value: 'Nome calendario' } );
	f.text ( { name : 'cal_name', mandatory: true } ); 
	f.label ( { value: 'Assegnato a:' } );
	f.select ( { name: 'uid', options: agenda.admin._users_opts, force_select : true } );
	f.label ( { value: 'Assegnato al gruppo:' } );
	f.select ( { name: 'gid', options: agenda.admin._groups_opts, force_select : true } );
	f.button ( { value: 'Salva', 'class' : 'green', onclick: function () { agenda.admin._check_form ( 'agenda_add_cal' ) }, nonl : true } );
	f.button ( { value: 'Reset', 'class' : 'yellow', onclick: function ( f )
			 { 
				var act = f.get_value ( 'action' );
				f.clear (); 
				f.set_value ( 'action', act );
			} } );
	f.set ( agenda.admin._add_form_div );

	if ( data )
	{
		data.iterate ( function ( v, k ) { f.set_value ( k, v ); } );
		agenda.admin.tabs.send_event ( agenda.admin._add_form_div, 'click' );
	}
};

agenda.admin._check_form = function ( form_name, cback )
{
	var f = liwe.form.get ( form_name );
	if ( ! f.check () ) return;
	var vals = f.get_values ();
	if ( vals.get ( 'id_cal', 0 ) > 0 )
	{
		var conf = confirm ( 'Vuoi modificare il calendario esistente ( id : ' + vals.id_cal + ' ) ?' );
		if ( ! conf ) return;
	}
	agenda.admin.ajax ( vals, function ( data ) 
		{ 
			console.debug ( "FORM: %s - %o" , form_name, data ); 
			agenda.admin._filter_apply ();
			agenda._update_cals_cache ();
		} ); 
};

agenda.admin.add_cal = function ( dest_div, id )
{
	if ( ! id ) 
	{
		agenda.admin._create_add_cal_form ();
		return;
	}
	var cals = agenda._cache.get ( 'calendars', [] );
	var name = agenda._calendars.get ( id );
	v = cals [ id ];
	agenda.admin._create_add_cal_form ( v );
	agenda.show_calendar ( v.id_cal );//name
};

agenda.admin.del_cal = function ( dest_div, id )
{
	if ( ! id ) return;
	var conf = confirm ( "Vuoi cancellare il calendario ?" );
	if ( ! conf ) return;
	agenda.admin.ajax ( { 'action' : 'agenda.admin.del_calendar', 'id_cal' : id } , function ( data ) 
			{
				system.admin.do_alert ( "Calendario eliminato", liwe.utils.notifier.mode.NOTICE );
				agenda.admin._filter_apply ();
				var AG, lst = agenda._cur_cal_shown ();
				//clear html page if the deleted calendar is shown
				lst.iterate ( function ( v )
					{
						if ( ! id == v ) return;
						AG = agenda.get_instance ( id );
						cnt = agenda.templates [ 'cal-cnt' ];
						if ( $( 'AG-cal-cnt' ) ) $( 'AG-cal-cnt', cnt );
						//FIXME: must be fixed for multi instances support
						//if ( $( AG._dest_div ) ) $( AG._dest_div, '' );
					} );
				agenda._update_cals_cache ();

			} );
};
